package com.eqdd.yiqidian.utils;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;

import com.eqdd.yiqidian.ListItem89Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.GridBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rongyun.message.link.LinkContent;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.WechatFavorite;
import cn.jiguang.share.wechat.WechatMoments;

/**
 * @author吕志豪 .
 * @date 17-12-31  上午10:00.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ShareDialogUtil {

    public static void showLink(BaseActivity baseActivity, LinkContent linkContent) {
        BottomSheetDialog shareDialog = new BottomSheetDialog(baseActivity);
        ListItem89Custom inflate = DataBindingUtil.inflate(baseActivity.getLayoutInflater(), R.layout.list_item_89, null, false);
        inflate.recyclerView.setLayoutManager(new GridLayoutManager(baseActivity, 4));
        inflate.recyclerView.setNestedScrollingEnabled(false);
        inflate.recyclerView.addItemDecoration(
                new GridSpacingItemDecoration(4, 50, true));
        ArrayList<GridBean> gridBeans = new ArrayList<>();
        gridBeans.add(new GridBean("发送给好友", R.mipmap.share_friend));
        gridBeans.add(new GridBean("工作圈", R.mipmap.share_eqd));
        gridBeans.add(new GridBean("我的收藏", R.mipmap.share_shoucang));
        gridBeans.add(new GridBean("用浏览器打开", R.mipmap.share_liulanqi));
        gridBeans.add(new GridBean("微信", R.mipmap.wechat));
        gridBeans.add(new GridBean("微信收藏", R.mipmap.wechat_fav));
        gridBeans.add(new GridBean("朋友圈", R.mipmap.wechat_moment));
        gridBeans.add(new GridBean("QQ", R.mipmap.qq));
        gridBeans.add(new GridBean("qq空间", R.mipmap.qzone));
        gridBeans.add(new GridBean("新浪微博", R.mipmap.weibo));
        SlimAdapterEx slimAdapterEx = SlimAdapterEx.create().register(R.layout.grid_item, new SlimInjector<GridBean>() {
            @Override
            public void onInject(GridBean data, IViewInjector injector) {
                injector.text(R.id.ItemText, data.getTitle())
                        .image(R.id.ItemImage, data.getResId())
                        .gone(R.id.red_dot);
            }
        }).attachTo(inflate.recyclerView).updateData(gridBeans);
        ItemClickSupport.addTo(inflate.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    switch (position) {
                        case 0:
                            ShareUtil.share2EQDLink(baseActivity, linkContent);
                            break;
                        case 1:
                            ShareUtil.share2WeXin(linkContent.getUrl());
                            break;
                        case 2:
                            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_COLLECTION)
                                    .params("owner", AppConstent.user.getGuid())
                                    .params("type", 10)
                                    .params("title", linkContent.getTitle())
                                    .params("url", linkContent.getUrl())
                                    .params("source", "易企阅")
                                    .params("sourceOwner", linkContent.getSourceOwner())
                                    .execute(new DialogCallBack<HttpResult>(baseActivity) {
                                        @Override
                                        public void onSuccess(Response<HttpResult> response) {
                                            HttpResult httpResult = response.body();
                                            ToastUtil.showShort(httpResult.getMsg());
                                            if (httpResult.getStatus() == 200) {

                                            }
                                        }
                                    });
                            break;
                        case 3:
                            Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(linkContent.getUrl()));
                            baseActivity.startActivity(in);
                        case 4:
                            ShareUtil.share2WeXin(linkContent.getUrl());
                            break;
                        case 5:
                            ShareUtil.shareTo(WechatFavorite.Name, linkContent.getUrl(), linkContent.getImgUrl());
                            break;
                        case 6:
                            ShareUtil.shareTo(WechatMoments.Name, linkContent.getUrl(), linkContent.getImgUrl());
                            break;
                        case 7:
                            ShareUtil.shareTo(QQ.Name, linkContent.getUrl(), linkContent.getImgUrl());
                            break;
                        case 8:
                            ShareUtil.shareTo(QZone.Name, linkContent.getUrl(), linkContent.getImgUrl());
                            break;
                        case 9:
                            ShareUtil.share2Sina(linkContent.getUrl());
                            break;

                    }
                    shareDialog.dismiss();
                });
        shareDialog.setContentView(inflate.getRoot());
        shareDialog.setCancelable(true);
        shareDialog.setCanceledOnTouchOutside(true);
        shareDialog.show();
    }

}
